<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>老师信息</title>
    <style>
        body {
            width: 920px;
            margin: 0 auto;
        }
        .teacher {
            clear: both;
        }
        .photo {
            margin-top: 20px;
            width: 140px;
            height: 140px;
            border-radius: 70px 70px 70px 70px;
            overflow: hidden;
            float: left;
        }
        .content {
            margin-left: 60px;
            width: 580px;
            float: left;
        }
        .keyinfo {
            font: bold 18px Arial;
        }
        .back {
            float: right;
            margin-top: 20px;
        }
        .back>a {
            text-decoration: none;
            color: blue;
        }
        .back+h2 {
            clear: both;
        }
    </style>
    <link rel="stylesheet" href="https://unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css">
</head>
<body>
    <div id="app" v-loading.fullscreen.lock="loading">
        <div class="back">
            <a href="/">返回首页</a>
        </div>
        <h2>{{ subject.name }}学科老师信息</h2>
        <hr>
        <div v-for="teacher in teachers" class="teacher">
            <div class="photo">
                <img :src="teacher.photo" width="140">
            </div>
            <div class="content">
                <p class="keyinfo">
                    {{ teacher.name }}&nbsp;&nbsp;
                    {{ teacher.sex | maleOrFemale }}&nbsp;&nbsp;
                    {{ teacher.birth }}
                </p>
                <p>
                    {{ teacher.intro }}
                </p>
                <p class="comment">
                    <a href="" @click.prevent="praise(teacher)">好评</a>
                    （<span>{{ teacher.goodCount }}</span>）&nbsp;&nbsp;
                    <a href="" @click.prevent="criticize(teacher)">差评</a>
                    （<span>{{ teacher.badCount }}</span>）
                </p>
            </div>
        </div>
        <div v-if="!loading && teachers.length == 0">
            <h3>暂时没有该学科老师信息</h3>
        </div>
    </div>
    <script src="https://unpkg.com/vue@2.6.11/dist/vue.min.js"></script>
    <script src="https://unpkg.com/element-ui@2.13.0/lib/index.js"></script>
    <script>
        let app = new Vue({
            el: '#app',
            data: {
                subject: {},
                teachers: [],
                loading: true
            },
            filters: {
                maleOrFemale(value) {
                    return value ? '男' : '女'
                }
            },
            created() {
                fetch('/teachers/' + location.search)
                    .then(resp => resp.json())
                    .then(json => {
                        this.loading = false
                        if (json.code == 20000) {
                            this.subject = json.subject
                            this.teachers = json.teachers
                        } else {
                            this.$message.error(json.message)
                        }
                    })
            },
            methods: {
                praise(teacher) {
                    fetch('/praise/?tno=' + teacher.no)
                        .then(resp => resp.json())
                        .then(json => {
                            if (json.code == 10000) {
                                teacher.goodCount += 1
                            }
                        })
                },
                criticize(teacher) {
                    fetch('/criticize/?tno=' + teacher.no)
                        .then(resp => resp.json())
                        .then(json => {
                            if (json.code == 10000) {
                                teacher.badCount += 1
                            }
                        })
                }
            }
        })
    </script>
</body>
</html>